﻿<%@ Page Title="群组权限管理" Language="C#" AutoEventWireup="true" CodeBehind="Group.aspx.cs"
    Inherits="func_Group" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>群组权限管理</title>
    <link rel="stylesheet" type="text/css" href="../../css/theme.css" />
    <link rel="stylesheet" type="text/css" href="../../css/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../css/icon.css" />
    <script src="../../js/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../js/Json.js" type="text/javascript"></script>
    <script src="../../js/Common.js" type="text/javascript"></script>
    
    <style type="text/css">
        .ftitle
        {
            font-size: 14px;
            font-weight: bold;
            padding: 5px 0;
            margin-bottom: 10px;
            border-bottom: 1px solid #ccc;
        }
        .fitem
        {
            margin-bottom: 5px;
        }
        .fitem label
        {
            display: inline-block;
            width: 80px;
        }
        .searchBlock
        {
            padding-left: 20px;
        }
        .searchBlock span
        {
        }
        
        .searchTitle
        {
            padding-right: 2px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="container" style="width: 100%; padding: 5px">
        <div id="toolbar" style="text-align: left; padding-bottom: 10px">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="pageJs.search()">搜索</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" id="add" onclick="pageJs.add()">
                新增</a> <a href="javascript:void(0)" class="easyui-linkbutton" id="edit" onclick="pageJs.edit()">
                    编辑</a> <a href="javascript:void(0)" class="easyui-linkbutton" id="del" onclick="pageJs.del()">
                        删除</a>
            <div style="padding-top: 5px">
                <span class="searchTitle">群组代码</span>
                <input style="width: 110px" id="sGROUPID" /><span class="searchBlock"> <span class="searchTitle">
                    群组名称</span>
                    <input style="width: 110px" id="sGROUPNAME" />
                </span>
            </div>
        </div>
        <div id="divTable" style="text-align: left">
            <div id="divGrid" style="float: left; width: 430px">
                <table id="dg" title="群组列表" style="height: 350px; width: 410px" pagination="true"
                    autorowheight="false" rownumbers="true" fitcolumns="false" singleselect="true">
                    <thead>
                        <tr>
                            <th field="GROUP_ID" width="80">
                                群组代码
                            </th>
                            <th field="GROUP_NAME" width="120">
                                群组名称
                            </th>
                            <th field="GROUP_DESC" width="150">
                                群组说明
                            </th>
                        </tr>
                    </thead>
                </table>
                <div id="pnlDlg" style="display: none">
                    <div id="dlg" style="width: 400px; height: 250px; padding: 10px 20px;" title="add"
                        data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true" buttons="#dlg-buttons">
                        <div class="ftitle">
                            群组明细</div>
                        <div class="fitem">
                            <label>
                                群组代码:</label>
                            <input name="GROUP_ID" id="GROUP_ID" class="easyui-validatebox" style="ime-mode: disabled;"
                                disabled="disabled" />
                        </div>
                        <div class="fitem">
                            <label>
                                群组名称:</label>
                            <input name="" id="GROUP_NAME" class="easyui-validatebox" />
                            <input id="GROUP_KEY" type="hidden" />
                        </div>
                        <div class="fitem">
                            <label>
                                群组说明:</label>
                            <input name="GROUP_DESC" id="GROUP_DESC" />
                        </div>
                    </div>
                    <div id="dlg-buttons">
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="pageJs.doSave()">
                            保存</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel"
                                onclick="javascript:$('#dlg').dialog('close')">取消</a>
                    </div>
                </div>
            </div>
            <div style="float: left; text-align: left;">
                <div style="padding-bottom: 5px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton  l-btn" id="editAuth" onclick="pageJs.editTree()">
                        编辑权限</a>
                </div>
                <div id="divTree" style="overflow: auto;">
                    <div>
                        厂别：
                        <select class="combo" id="dllFactory" name="dllFactory" style="width: 200px">
                        </select>
                    </div>
                    <ul id="tree">
                    </ul>
                </div>
                <div id="divtreebtn" style="display: none; padding-top: 5px">
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="pageJs.doSaveTree()">
                        保存</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel"
                            onclick="pageJs.cancel();">取消</a>
                </div>
            </div>
        </div>
    </div>
    </form>
    <script type="text/javascript">
        var pageJs = {
            url: "",
            resize: function (h, w) {
                $('#container').height(h).width(w - 10);
                var contentH = h - $("#toolbar").outerHeight() - 10;
                if (contentH < 120) contentH = 120;
                $("#dg").datagrid("resize", { height: contentH });
                var contentW = $("#divTable").width() - 430;
                if (contentW < 150) contentW = 150;
                $("#divTree").height(contentH - 70).width(contentW - 15);
            },
            validate: function () {
                var GROUPid = $.trim($("#GROUP_ID").val());
                var GROUPname = $.trim($("#GROUP_NAME").val());
                var desc = $.trim($("#GROUP_DESC").val());
                if (GROUPid == "" || GROUPid.length > 20)
                    return "群组代码为必须项，最长20字符！";
                if (GROUPname == "" || GROUPname.length > 20)
                    return "群组名称为必须项，最长20字符！";
                if (desc.length > 100)
                    return "群组说明最长100字符！";

                return "";

            },
            cancel: function () {
                var groupkey = $("#dg").datagrid("getSelected").GROUP_KEY;
                $('#tree').tree({
                    url: '../../ashx/System/hGroup.ashx?action=getgroupmenu&showall=false&groupkey=' + groupkey,
                    animate: true,
                    checkbox: false,
                    onLoadSuccess: function (node, data) {
                        if (data[0]) {
                            var folder = eval(data[0].attributes.ACTION_ID);
                            $.each(folder, function (index, item) {
                                var foNode = $("#tree").tree("find", item);
                                if (foNode && foNode.target) $(foNode.target).find(".tree-file").attr("class", "tree-folder");
                            });
                        }

                    }

                });
                $('#dllFactory').combogrid("disable").combogrid("grid").datagrid("load", { groupkey: groupkey });
                $('#divtreebtn').hide();

            },
            editTree: function () {
                if ($('#divtreebtn').css("display") == "none") {
                    $('#tree').tree({
                        url: '../../ashx/System/hGroup.ashx?action=getgroupmenu&showall=true&groupkey=' + escape($("#dg").datagrid("getSelected").GROUP_KEY),
                        animate: true,
                        checkbox: true,
                        onLoadSuccess: function (node, data) {
                            $('#dllFactory').combogrid("enable");
                            $('#divtreebtn').show();
                            var folder = eval(data[0].attributes.ACTION_ID);
                            $.each(folder, function (index, item) {
                                var foNode = $("#tree").tree("find", item);
                                if (foNode && foNode.target) $(foNode.target).find(".tree-file").attr("class", "tree-folder");

                            });
                        }
                    });

                }
            },

            getFatherKey: function (node, keys) {
                var parent = $('#tree').tree("getParent", node.target);
                if (parent == null) return;
                else {
                    var pData = $('#tree').tree("getData", parent.target);
                    pKey = pData.attributes.key;
                    if ($.inArray(pKey, keys) == -1) keys.push(pKey);
                    pageJs.getFatherKey(parent, keys);
                }

            },
            doSaveTree: function () {
                var nodes = $("#tree").tree("getChecked");
                //菜单
                var menus = [];

                $.each(nodes, function (i, item) {
                    if (item.attributes.ACTION_ID != null && item.attributes.ACTION_ID.length > 0) {
                        //只保存操作权限节点
                        menus.push(item.attributes);
                    }
                });
                //厂别
                var fdata = $('#dllFactory').combogrid("grid").datagrid("getSelections")
                var groupkey = escape($("#dg").datagrid("getSelected").GROUP_KEY);
                var parm = { GROUP_KEY: groupkey, menus: menus, factorys: fdata };
                $.post('../../ashx/System/hGroup.ashx?action=editgroupmenu', { data: JSON.stringify(parm) }, function (result) {
                    if (result.Success) {
                        //save tree
                        BasicJs.showMessage("保存成功！", true);
                        //reload tree
                        $('#tree').tree({
                            url: '../../ashx/System/hGroup.ashx?action=getgroupmenu&showall=false&groupkey=' + groupkey,
                            animate: true,
                            checkbox: false,
                            onLoadSuccess: function (node, data) {
                                if (data[0]) {
                                    var folder = eval(data[0].attributes.ACTION_ID);
                                    $.each(folder, function (index, item) {
                                        var foNode = $("#tree").tree("find", item);
                                        if (foNode && foNode.target) $(foNode.target).find(".tree-file").attr("class", "tree-folder");
                                    });
                                }

                                $('#divtreebtn').hide();
                            }
                        });
                        //reload factory                             
                        $('#dllFactory').combogrid("disable").combogrid("grid").datagrid("load", { groupkey: groupkey });

                    }
                    else {
                        //error
                        BasicJs.showMessage(result.Message);
                    }
                    //close dialog

                }, "json"
             );

            },

            doSave: function () {
                var valMsg = this.validate();
                if (valMsg != "") {
                    BasicJs.showMessage(valMsg);
                    return;
                }
                var parm = { GROUP_ID: $.trim($("#GROUP_ID").val()), GROUP_NAME: $.trim($("#GROUP_NAME").val()),
                    GROUP_DESC: $.trim($("#GROUP_DESC").val()), GROUP_KEY: $("#GROUP_KEY").val()
                };
                var action = "add";
                if ($("#GROUP_KEY").val() != "0") action = "edit";
                $.post('../../ashx/System/hGroup.ashx?action=' + action, { data: JSON.stringify(parm) }, function (result) {
                    if (result.Success) {
                        if ($("#GROUP_KEY").val() != "0") {
                            //edit
                            var callback = function () { $('#dlg').dialog('close'); };
                            BasicJs.showMessage("保存成功！", true, callback);

                        }
                        else {//add
                            BasicJs.showMessage("保存成功！", true);
                            pageJs.initAdd();
                            setTimeout('$("#GROUP_NAME").focus()', 100);
                        }
                        $('#dg').datagrid('reload');

                    }
                    else {
                        //error

                        if (result.Message == "此群组已被删除，是否还原？") {
                            $.messager.confirm('还原群组', result.Message, function (r) {
                                if (r) {
                                    $.post(pageJs.url + "?action=revert", { id: $.trim($("#GROUP_ID").val()) }, function (result) {
                                        if (result.Success) {
                                            $('#dg').datagrid('reload');    // reload the user data
                                            pageJs.initAdd();
                                            BasicJs.showMessage("还原成功！", true);
                                        } else {
                                            if (result.Message) BasicJs.showMessage(result.Message);
                                        }
                                    }, 'json');
                                }
                            });
                        }
                        else BasicJs.showMessage(result.Message);
                    }
                    //close dialog

                }, "json"
             );

            },

            initAdd: function () {
                $("#GROUP_KEY").val("0");
                $("#GROUP_ID").val("").removeAttr("disabled");
                $("#GROUP_DESC").val("");
                $("#GROUP_NAME").val("");
            },
            add: function () {
                this.initAdd();
                var pos = BasicJs.getCenter("#dlg");
                $("#pnlDlg").show();
                var position = BasicJs.autoDialogSize(250, 400);
                $('#dlg').dialog(pos).dialog({
                    left: position.Left,
                    top: position.Top,
                    height: position.Height,
                    width: position.Width,
                    "onOpen": function () {
                        setTimeout(' $("#GROUP_ID").focus();', 100);
                    }
                }).dialog('open').dialog('setTitle', '新增群组权限');

            },

            edit: function () {

                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    $("#GROUP_KEY").val(row.GROUP_KEY);
                    $("#GROUP_ID").val(row.GROUP_ID).attr("disabled", "disabled");
                    $("#GROUP_NAME").val(row.GROUP_NAME);
                    $("#GROUP_DESC").val(row.GROUP_DESC);
                    var pos = BasicJs.getCenter("#dlg");
                    $("#pnlDlg").show();
                    var position = BasicJs.autoDialogSize(250, 400);
                    $('#dlg').dialog(pos).dialog({
                        left: position.Left,
                        top: position.Top,
                        height: position.Height,
                        width: position.Width,

                        "onOpen": function () {
                            setTimeout(' $("#GROUP_NAME").focus();', 100);
                        }
                    }).dialog('open').dialog('setTitle', '编辑群组权限');
                }
                else { BasicJs.showMessage("未选中记录！"); }

            },

            del: function () {
                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    $.messager.confirm('删除群组权限', '您确定要刪除选中的群组权限吗?', function (r) {
                        if (r) {
                            $.post(pageJs.url + "?action=del", { data: JSON.stringify(row) }, function (result) {
                                if (result.Success) {
                                    $('#dg').datagrid('reload');    // reload 
                                    BasicJs.showMessage("刪除成功！", true);
                                }
                                else {
                                    if (result.Message) BasicJs.showMessage(result.Message);
                                }
                            }, 'json');
                        }
                    });
                }
                else { BasicJs.showMessage("未选中记录！"); }


            },
            search: function () {
                $('#dg').datagrid('load',
                 {
                     sGROUPID: $.trim($("#sGROUPID").val()),
                     sGROUPName: $.trim($('#sGROUPNAME').val())
                 }
            );

            },

            dataFilter: function (data) {
                return data;
            }

        }

        $(function () {
            //授权
            var funKey = window.name.substring(3);
            BasicJs.authorize("#add", funKey, "ADD");
            BasicJs.authorize("#edit", funKey, "EDIT");
            BasicJs.authorize("#del", funKey, "DELETE");
            BasicJs.authorize("#editAuth", funKey, "EDITAUTH");
            $('#dg').datagrid(
                 {
                     url: "../../ashx/System/hGroup.ashx?action=get",
                     loadMsg: "正在加载数据，请稍后...",
                     onLoadSuccess: function (data) {
                         if (data.total > 0) {
                             $('#dg').datagrid("selectRow", 0);
                         }
                     },
                     onSelect: function (rowIndex, rowData) {
                         //群组对应的菜单权限
                         $('#divtreebtn').hide();
                         if (rowData) {
                             //厂别控件初始化
                             $('#dllFactory').combogrid({
                                 delay: 500,
                                 panelWidth: 250,
                                 panelHeight: 300,
                                 mode: 'remote',
                                 url: '../../ashx/System/hGroup.ashx?action=getFactory',
                                 idField: 'FACTORY_ID',
                                 textField: 'FACTORY_NAME',
                                 fit: true,
                                 editable: false,
                                 queryParams: { groupkey: rowData.GROUP_KEY },
                                 disabled: true,
                                 multiple: true,
                                 pagination: false, //是否分页
                                 onLoadSuccess: function (data) {
                                     var values = [];
                                     $.each(data.rows, function (index, item) {
                                         if (item.ENABLE) values.push(item.FACTORY_ID);
                                     });

                                     $('#dllFactory').combogrid("clear").combogrid("setValues", values);
                                 },
                                 columns: [[
                                    { field: 'ck', checkbox: true },
                                    { field: 'FACTORY_ID', title: '厂别代码', width: 100, sortable: false },
                                    { field: 'FACTORY_NAME', title: '厂别名称', width: 100, sortable: false }
                                ]]
                             });

                             $('#tree').tree({
                                 url: '../../ashx/System/hGroup.ashx?action=getgroupmenu&showall=false&groupkey=' + escape(rowData.GROUP_KEY),
                                 checkbox: false,
                                 animate: true,
                                 onLoadSuccess: function (node, data) {
                                     if (data[0]) {
                                         var folder = eval(data[0].attributes.ACTION_ID);
                                         $.each(folder, function (index, item) {
                                             var foNode = $("#tree").tree("find", item);
                                             if (foNode && foNode.target) $(foNode.target).find(".tree-file").attr("class", "tree-folder");
                                         });
                                     }

                                 }

                             });
                         }


                     },
                     loadFilter: pageJs.dataFilter
                 }
            );
            pageJs.resize($(window).height(), $(window).width());
            pageJs.url = '../../ashx/System/hGroup.ashx';
        });  
        
       
    
    </script>
</body>
</html>
